package wangshuai.risk.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import publics.model.Ratio;
import publics.util.Page;
import wangshuai.risk.dao.RatioDao;
import wangshuai.risk.service.RatioService;
@Component("ratioService")
public class RatioServiceImpl implements RatioService {

	private RatioDao ratioDao;
	
	public RatioDao getRatioDao() {
		return ratioDao;
	}

	@Resource(name="ratioDao")
	public void setRatioDao(RatioDao ratioDao) {
		this.ratioDao = ratioDao;
		System.out.println("ratioDaoImpl inject in RatioServiceImpl!");
	}


	/**
	 * 查询所有财务比率表信息
	 * @param offset 起始条目偏移量（offset == （页码-1）*pageSize+1  ）
	 * @param pageSize  查询每页的条目数
	 * @return 全部财务比率表
	 */
	public Page findByPage(Page page) {
		StringBuffer sql = new StringBuffer();
		sql.append("from Ratio r ");
		if(page.getConditions().size() > 0){//这里为模糊查询的 sql 语句拼接 请仔细体会
			sql.append(" where ");  
			for(int i = 0; i < page.getConditions().size(); ){
				sql.append("( r.year like '%").append(page.getConditions().get(i)).append("%'")//查询条件一 
				.append(" or r.ent in ( select id from Enterprise e where e.name like '%").append(page.getConditions().get(i)).append("%' )) ");
				
				i++;
				if(i < page.getConditions().size()){// 如果条件不止一个 进行连续拼接
					sql.append(" and ");
				}
			}
		}
		System.out.println("sql ="+sql);// 测试用，请观察拼接出的sql语句，加强理解
		int pageSize = page.getLimit();
		int offset = page.getStart();//-1)*pageSize;
		page.setRoot(this.getRatioDao().findByPage(sql.toString(),offset,pageSize));//把查询结果放入root中
		page.setTotalProperty(this.getRatioDao().findByCount(sql.toString()));   //把数据总数放入totalProperty中	
		
		return page;
	}

	public boolean deleteRatio(Ratio ratio) {		
			return this.getRatioDao().delete(ratio);		
	}
	
	public boolean deleteById(Integer id) {
		return this.getRatioDao().deleteById(id);
	}

	public List<Ratio> findByAppId(Integer appId) {
		return this.getRatioDao().findByAppId(appId);
	}

	public Ratio findById(Integer id) {
		return this.getRatioDao().findById(id);
	}

	public Integer savaRatio(Ratio ratio) {
		return this.getRatioDao().sava(ratio);
	}

	public boolean updateRatio(Ratio ratio) {
		return this.getRatioDao().update(ratio);
	}

}
